package com.lemon.smartsafetynew.ui.adapter.work;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.entity.main.MainPanelButton;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafetynew.entity.event.work.WorkUIEvent;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/9/30.
 */

public class WorkSecondaryOptionItemViewRecNew implements AdapterItem<MainRcvItemBase<List<MainPanelButton>>> {

    private RecyclerView mRecyclerView;

    private WorkOptionItemAdapterNew mAdapter;

    /**
     * @return item布局文件的layoutId
     */
    @Override
    public int getLayoutResId() {
        return R.layout.view_recycler_view;
    }

    /**
     * 初始化views
     */
    @Override
    public void bindViews(View root) {
        mRecyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);
    }

    @Override
    public void setViews() {

    }

    /**
     * 根据数据来设置item的内部views
     *
     * @param itemBase 数据list内部的model
     * @param position 当前adapter调用item的位置
     */
    @Override
    public void handleData(MainRcvItemBase<List<MainPanelButton>> itemBase, int position) {

        if (RcvViewType.MainRcvViewType.TYPE_SECONDARY_OPTION != itemBase.getViewType()) {
            return;
        }

        final List<MainPanelButton> items = itemBase.getData();
        LinearLayoutManager manager = new LinearLayoutManager(mRecyclerView.getContext());
        mAdapter = new WorkOptionItemAdapterNew(items);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
    }

    public class WorkOptionItemAdapterNew extends LemonRcvAdapter<MainPanelButton> {


        public WorkOptionItemAdapterNew(@Nullable List<MainPanelButton> data) {
            super(data);
        }


        @Override
        public int getItemCount() {
            return getData().size();
        }

        @NonNull
        @Override
        public AdapterItem createItem(Object type) {
            return new MainOptionItemRecItem();
        }

        public class MainOptionItemRecItem implements AdapterItem<MainPanelButton> {


            @Bind(R.id.image_card)
            ImageView imageCard;

            private int mPosition;
            private MainPanelButton mainPanelButton;

            @Override
            public int getLayoutResId() {
                return R.layout.item_image;
            }

            @Override
            public void bindViews(View root) {
                ButterKnife.bind(this, root);
            }


            @Override
            public void setViews() {
                imageCard.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        WorkUIEvent event = new WorkUIEvent(mainPanelButton);
                        AppService.getBus().post(event);
                    }
                });

            }

            @Override
            public void handleData(MainPanelButton data, int position) {

                mainPanelButton = data;
                mPosition = position;

                imageCard.setBackgroundResource(data.getIconRes());

            }
        }
    }
}
